/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
   
/*
 * ExoneracionesAgregarFrame.java
 *
 * Created on 09/22/2008, 01:50:12 PM
 */
package org.contabilidad.exoneraciones;

import org.mybeans.Sesion;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author avbravo
 */
public class ExoneracionesAgregarFrame extends javax.swing.JFrame {

    Sesion sesion;

    /** Creates new form ExoneracionesAgregarFrame */
    public ExoneracionesAgregarFrame() {
        initComponents();
        sesion = new Sesion();
        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        jXDatePickerFecha.setDate(mydate);
        jXDatePickerFecha.setFormats(new SimpleDateFormat("dd/MM/yyyy"));
        /*
         * Fondo
         */

         
         
         

        jXPanel1.setOpaque(false);
        setContentPane(jXPanel1);
    /*
     * fondo
     */




    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldIdExoneracion = new javax.swing.JTextField();
        jTextFieldAutorizadoPor = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDetalle = new javax.swing.JTextArea();
        jButtonGuardar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jXDatePickerFecha = new org.jdesktop.swingx.JXDatePicker();
        jTextFieldValor = new javax.swing.JTextField();
        jTextFieldPorcentaje = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar Exoneracion");

        jXPanel1.setBorder(new org.jdesktop.swingx.border.DropShadowBorder());

        jLabel1.setText("Numero de Acuerdo");

        jLabel2.setText("Fecha");

        jLabel3.setText("Autorizado por");

        jLabel4.setText("Descripcion");

        jLabel5.setText("Valor");

        jLabel6.setText("Porcentaje (%)");

        jTextAreaDetalle.setColumns(20);
        jTextAreaDetalle.setRows(5);
        jScrollPane1.setViewportView(jTextAreaDetalle);

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jTextFieldValor.setText("0.0");

        jTextFieldPorcentaje.setText("0");
        jTextFieldPorcentaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPorcentajeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jXPanel1Layout.createSequentialGroup()
                                .addGap(65, 65, 65)
                                .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(24, 24, 24)
                                .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jXPanel1Layout.createSequentialGroup()
                                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jXDatePickerFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldIdExoneracion, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jXPanel1Layout.createSequentialGroup()
                                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel6))
                                .addGap(47, 47, 47)
                                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldAutorizadoPor, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(24, Short.MAX_VALUE))
                    .addComponent(jLabel5)))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIdExoneracion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jXDatePickerFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldAutorizadoPor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(38, 38, 38)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed

        dispose();
}//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed

        try {

            String IdExoneracion = this.jTextFieldIdExoneracion.getText();
            if (IdExoneracion == null || IdExoneracion.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el numero de acuerdo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldIdExoneracion.requestFocus();
                return;
            }

            String AutorizadoPor = this.jTextFieldAutorizadoPor.getText();
            if (AutorizadoPor == null || AutorizadoPor.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el nombre de la persona que autoriza", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldAutorizadoPor.requestFocus();
                return;
            }
            String Detalle = this.jTextAreaDetalle.getText();
            if (Detalle == null || Detalle.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese la descripcion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextAreaDetalle.requestFocus();
                return;
            }
            String sValor = this.jTextFieldValor.getText();
            if (sValor == null || sValor.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el valor de la exoneracion", "Mensaje", JOptionPane.WARNING_MESSAGE);
            }
            double Valor = Double.parseDouble(sValor);
            if (Valor < 0) {
                JOptionPane.showMessageDialog(this, "El valor no debe ser negativo.", "Mensaje", JOptionPane.WARNING_MESSAGE);
                this.jTextFieldValor.setText("");
                jTextFieldValor.requestFocus();
                return;
            }

            String sPorcentaje = this.jTextFieldPorcentaje.getText();
            if (sPorcentaje == null || sPorcentaje.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el porcentaje de la exoneracion", "Mensaje", JOptionPane.WARNING_MESSAGE);
            }
            double Porcentaje = Double.parseDouble(sPorcentaje);
            if (Valor < 0) {
                JOptionPane.showMessageDialog(this, "El valor no debe ser negativo.", "Mensaje", JOptionPane.WARNING_MESSAGE);
                this.jTextFieldValor.setText("");
                jTextFieldValor.requestFocus();
                return;
            }
            java.util.Date FechaDate = jXDatePickerFecha.getDate();
            java.sql.Date FechaExoneracion = new java.sql.Date(FechaDate.getTime());
            if (FechaExoneracion == null) {
                JOptionPane.showMessageDialog(this, "Ingrese la fecha del acuerdo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jXDatePickerFecha.requestFocus();
                return;
            }


            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String sqlquery = "select * from exoneraciones where idmunicipio = ? and idexoneracion =?";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setString(2, IdExoneracion);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();

            if (x != 0) {
                JOptionPane.showMessageDialog(this, "Existe una exoneracion con ese número de acuerdo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

                return;
            }

            String sql = "INSERT into exoneraciones VALUES(?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement prest = sesion.con.prepareStatement(sql);

            prest.setString(1, IdExoneracion);
            prest.setDate(2, FechaExoneracion);
            prest.setString(3, AutorizadoPor);
            prest.setString(4, Detalle);
            prest.setDouble(5, Porcentaje);
            prest.setDouble(6, Valor);
            prest.setString(7, "si");
            prest.setString(8, sesion.getIdmunicipio());
            prest.setString(9, sesion.getUsername());

            java.util.Calendar startDate = GregorianCalendar.getInstance();
            startDate.getTime();

            java.util.Calendar ca = java.util.Calendar.getInstance();
            java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());

            prest.setDate(10, mydate);

            int count = prest.executeUpdate();
            sesion.con.close();
            JOptionPane.showMessageDialog(this, "Exoneracion guardada exitosamente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

        } catch (Exception ex) {
            System.out.println("Error" + ex);
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

}//GEN-LAST:event_jButtonGuardarActionPerformed

    private void jTextFieldPorcentajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPorcentajeActionPerformed
}//GEN-LAST:event_jTextFieldPorcentajeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ExoneracionesAgregarFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaDetalle;
    private javax.swing.JTextField jTextFieldAutorizadoPor;
    private javax.swing.JTextField jTextFieldIdExoneracion;
    private javax.swing.JTextField jTextFieldPorcentaje;
    private javax.swing.JTextField jTextFieldValor;
    private org.jdesktop.swingx.JXDatePicker jXDatePickerFecha;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    // End of variables declaration//GEN-END:variables
}
